﻿#ifndef LOWENERGYINFOMODEL_H
#define LOWENERGYINFOMODEL_H

#include <QAbstractListModel>
#include "QtChCharset.h"
#include <QtBluetooth/QtBluetooth>
#include <QBluetoothDeviceInfo>


class LowEnergyInfoModel : public QAbstractListModel
{
    Q_OBJECT
public:
    enum EnumRowType{
        EnumRowType_Unkown,        
        EnumRowType_Service,
        EnumRowType_Characteristic,
        EnumRowType_Descriptor
    };
    Q_ENUMS(EnumRowType)
    enum PropertyType {
        Unknown = 0x00,
        Broadcasting = 0x01,
        Read = 0x02,
        WriteNoResponse = 0x04,
        Write = 0x08,
        Notify = 0x10,
        Indicate = 0x20,
        WriteSigned = 0x40,
        ExtendedProperty = 0x80
    };
    Q_ENUM(PropertyType)
public:
    static LowEnergyInfoModel * Singleton();
public:
    enum BluetoothDeviceRole {
        serviceNameRole = Qt::DisplayRole,
        rowTypeRole = Qt::UserRole,
        serviceUuidRole,
        characteristicNameRole,
        characteristicUuidRole,
        characteristicPropertyRole,
        descriptorNameRole,
        descriptorUuidRole,
        chDescValueRole,
        childItemCountRole,
    };
    Q_ENUM(BluetoothDeviceRole)

    LowEnergyInfoModel(QObject *parent = nullptr);

    virtual int rowCount(const QModelIndex & = QModelIndex()) const override;
    virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    virtual QHash<int, QByteArray> roleNames() const override;

    Q_INVOKABLE QVariantMap get(int row) const;
    // 增加
    Q_INVOKABLE void addService(const QString& serviceName, const QString& serviceUuid);
    Q_INVOKABLE void addCharacteristic(const QString& characteristicName, const QString& characteristicUuid, int property, QString value, const QString& serviceName, const QString& serviceUuid);
    Q_INVOKABLE void addDescriptor(const QString& descriptorName, const QString& descriptorUuid, QString value, const QString& characteristicName, const QString& characteristicUuid, const QString& serviceName, const QString& serviceUuid);
    // 更新
    Q_INVOKABLE void updateCharacteristicValue(const QString& characteristicUuid, const QString& serviceUuid, QString value);
    Q_INVOKABLE void updateDescriptorValue(const QString& descriptorUuid, const QString& characteristicUuid, const QString& serviceUuid, QString value);
    // 移除
    Q_INVOKABLE void remove(int row);
    // 清空
    Q_INVOKABLE void clear();
private:
    class RowData
    {
    public:
        EnumRowType rowType;
        // 服务信息
        QString serviceName;
        QString serviceUuid;
        // Characteristic
        QString characteristicName;
        QString characteristicUuid;
        int characteristicProperty;
        // Descriptor
        QString descriptorName;
        QString descriptorUuid;
        //Characteristic 或者 Descriptor的值
        QString chDescValue;
        // 子项数
        int childItemCount = 0;
    public:
        RowData():rowType(EnumRowType_Unkown) {}

    };

    QList<RowData> mBluetoothInfoList;
};

#endif // LOWENERGYINFOMODEL_H
